/**
 * de.herberlin.sudoku.Applet
 */
package de.herberlin.sudoku;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Locale;

import javax.swing.JApplet;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JProgressBar;

import de.herberlin.sudoku.gui.SimpleSwingPlayground;

/**
 * @author herberlin
 * @created 02.08.2006
 */
public class Applet extends JApplet implements ActionListener {

	private Sudoku sudoku = null;


	public void init() {
		SudokuFactory.newInstance();
		setLocale();
		this.setJMenuBar(getMenuBar());
		actionNew();
	}


	private void setLocale() {

		String param=getParameter("locale");
		Locale locale=Locale.getDefault();
		if ("de".equals(param)) {
			locale=Locale.GERMAN;
		} else if ("en".equals(param)) {
			locale=Locale.ENGLISH;
		}
		Main.locale=locale;

	}
	private JMenuBar getMenuBar() {
		JMenu menu =new JMenu("Sudoku");
		menu.add(createMenuItem("menu.file.new"));
		menu.add(createMenuItem("menu.file.resolve"));

		JMenuBar bar=new JMenuBar();
		bar.add(menu);
		return bar;
	}


	private JMenuItem createMenuItem(String key) {

		JMenuItem item=new JMenuItem();
		String title = Main.getMessage(key);
		int pos = title.indexOf("_");
		if (pos >= 0) {
			title = title.replaceAll("_", "");
			item.setMnemonic(title.charAt(pos));
		}
		item.setText(title);
		item.setActionCommand(key);
		item.addActionListener(this);

		return item;

	}
	public void actionPerformed(ActionEvent e) {

		if ("menu.file.new".equals(e.getActionCommand())) {
			actionNew();
		} else if ("menu.file.resolve".equals(e.getActionCommand())) {
			actionResolve();
		}
	}

	private void actionResolve() {
		if (Main.confirm(this,"message.confirm.resolve")) {
			Field[][] fields = sudoku.getFields();
			for (int m = 0; m < fields.length; m++) {
				for (int n = 0; n < fields[m].length; n++) {
					fields[m][n].setResolved();
				}
			}
		}
	}
	private void actionNew() {

		createProgressbar();
		new Thread() {
			public void run() {
				try {
					sudoku = SudokuFactory.getInstance().getSudoku();
					SudokuFactory.getInstance().setSeverity(sudoku,
							Constants.SEVERITY_MIDDLE);
					getContentPane().removeAll();
					getContentPane().add(new SimpleSwingPlayground(sudoku),
							BorderLayout.CENTER);
				} catch (InterruptedException e) {
					// Main.showError(null, e);
				}
			}
		}.start();
	}

	private void createProgressbar() {

		getContentPane().setLayout(new BorderLayout());
		getContentPane().removeAll();
		invalidate();


		JProgressBar progBar = new JProgressBar();
		progBar.setIndeterminate(true);
		progBar.setString("   " + Main.getMessage("message.wait") + "   ");
		progBar.setStringPainted(true);

		JPanel p = new JPanel(new BorderLayout());
		p.add(progBar, BorderLayout.CENTER);

		getContentPane().add(p, BorderLayout.NORTH);
		invalidate();
		doLayout();

	}

}
